package org.ht.demo.openapi.util;

import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Timestamp;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 判断字符串的util类
 *
 */
public class StringUtil {

	/**
	 * 计算CheckSum
	 *
	 * @param src
	 * @return
	 */
	public static int checksum(byte[] src) {
		int sum = 0;
		for (int i = 0; i < src.length; i++) {
			sum += src[i];
		}
		sum=sum&0xff;
		sum=0x100-sum;
		return sum&0xff;
	}

	public static String buileT(String tv){
		String rv="0";
		char[] schar=tv.toCharArray();
		if("0".equals(schar[0]+"")){
			rv="-"+Float.parseFloat(tv.substring(1,tv.length()))/10;
		}else if("1".equals(schar[0]+"")){
			rv=""+Float.parseFloat(tv.substring(1,tv.length()))/10;
		}
		return rv;
	}

	/**
	 *
	 * @description: 实体类转Map
	 * @author: Jeff
	 * @date: 2019年10月29日
	 * @param object
	 * @return
	 */
	public static Map<String, Object> entityToMap(Object object) {
		Map<String, Object> map = new HashMap<>();
		for (Class<?> superClass = object.getClass(); superClass != Object.class; superClass = superClass.getSuperclass()) {
			for (Field field : superClass.getDeclaredFields()) {
				try {
					boolean flag = field.isAccessible();
					field.setAccessible(true);
					Object o = field.get(object);
					if(o!=null&&Validator.isNotNull(o.toString()+"")){
						map.put(field.getName(), o);
						field.setAccessible(flag);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		return map;
	}

	/**
	 * 16进制不足0补齐
	 * @param str
	 * @param len
	 * @return
	 */
	public static String padLeft(String str,int len){
		String pad="0000000000000000";
		return len>str.length()&&len<=16&&len>=0?pad.substring(0,len-str.length())+str:str;
	}

	public static String padLeft32(String str,int len){
		String pad="00000000000000000000000000000000";
		return len>str.length()&&len<=32&&len>=0?pad.substring(0,len-str.length())+str:str;
	}

	/**
	 *
	 * @description: Map转实体类
	 * @author: Jeff
	 * @date: 2019年10月29日
	 * @param <T>
	 * @param map    需要初始化的数据，key字段必须与实体类的成员名字一样，否则赋值为空
	 * @param entity 需要转化成的实体类
	 * @return
	 */
	public static <T> T mapToEntity(Map<String, Object> map, Class<T> entity) {
		T t = null;
		try {
			t = entity.newInstance();
			for (Field field : entity.getDeclaredFields()) {
				if (map.containsKey(field.getName())) {
					boolean flag = field.isAccessible();
					field.setAccessible(true);
					Object object = map.get(field.getName());
					if (object != null && field.getType().isAssignableFrom(object.getClass())) {
						field.set(t, object);
					}
					field.setAccessible(flag);
				}
			}
			return t;
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		return t;
	}


	/**
	 * 16进制累加和，校验码 checksum-8算法
	 * @param data
	 * @return
	 */
	public static String makeChecksum(String data) {
		if (data == null || data.equals("")) {
			return "";
		}
		int total = 0;
		int len = data.length();
		int num = 0;
		while (num < len) {
			String s = data.substring(num, num + 2);
			total += Integer.parseInt(s, 16);
			num = num + 2;
		}
		/**
		 * 用256求余最大是255，即16进制的FF
		 */
		int mod = total % 256;
		String hex = Integer.toHexString(mod);
		len = hex.length();
		// 如果不够校验位的长度，补0,这里用的是两位校验
		if (len < 2) {
			hex = "0" + hex;
		}
		return hex;
	}

	/**
	 * 10进制累加和
	 * @param data
	 * @return
	 */
	public static String makeChecksum2(String data) {
		if (data == null || data.equals("")) {
			return "";
		}
		int sum = 0;
		for (int i = 0; i <= data.length() - 2; i = i + 2 ) {
			String s = data.substring(i,i + 2);
			int v = Integer.parseInt(s,16);
//			if(v >= 128){
//				v = -128 + (v - 128);
//			}
			sum += v;
		}
		if(sum > 0xff){
			sum = ~sum;
			sum = sum + 1;
		}
		return String.format("%x",sum & 0xff);
	}




	/**
	 * 16进制字符串转换成字节数组
	 * @param hexStr 16进制字符串
	 * @return 字节数组
	 */
	public static byte[] hexDecode(String hexStr) {
		if (hexStr == null || "".equals(hexStr)) {
			return null;
		}
		try {
			char[] cs = hexStr.toCharArray();
			return Hex.decodeHex(cs);
		} catch (DecoderException e) {
			e.printStackTrace();
		}
		return null;
	}


	/**
	 * ASCII转成字符串
	 *
	 * @param value
	 * @return
	 */
	public static String asciiToString(String value) {
		StringBuffer sbu = new StringBuffer();
		int lls = value.length() / 2;
		for (int i = 0; i < lls; i++) {
			sbu.append((char) Integer.parseInt(value.substring(i * 2, i * 2 + 2)));
		}
		return sbu.toString();
	}

	/**
	 * 正则校验
	 *
	 * @param regex 正则表达式字符串
	 * @param value 要匹配的字符串
	 * @return 正则校验结果
	 */
	public static boolean match(String regex, String value) {
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(value);
		return matcher.matches();
	}

	/**
	 * 将字符串转成ASCII
	 *
	 * @param value
	 * @return
	 */
	public static String stringToAscii(String value) {
		StringBuffer sbu = new StringBuffer();
		char[] chars = value.toCharArray();
		for (int i = 0; i < chars.length; i++) {
			sbu.append((int) chars[i]);
		}
		return sbu.toString();
	}

	/**
	 * @Title:hexString2Bytes
	 * @Description:16进制字符串转字节数组
	 * @param src  16进制字符串
	 * @return 字节数组
	 */
	public static byte[] hexString2Bytes(String src) {
		int l = src.length() / 2;
		byte[] ret = new byte[l];
		for (int i = 0; i < l; i++) {
			ret[i] = (byte) Integer.valueOf(src.substring(i * 2, i * 2 + 2), 16).byteValue();
		}
		return ret;
	}

	/**
	 * 字节数组转成16进制表示格式的字符串
	 *
	 * @param byteArray 需要转换的字节数组
	 * @return 16进制表示格式的字符串
	 **/
	public static String toHexString(byte[] byteArray) {
		if (byteArray == null || byteArray.length < 1)
			throw new IllegalArgumentException("this byteArray must not be null or empty");

		final StringBuilder hexString = new StringBuilder();
		for (int i = 0; i < byteArray.length; i++) {
			if ((byteArray[i] & 0xff) < 0x10)// 0~F前面不零
				hexString.append("0");
			hexString.append(Integer.toHexString(0xFF & byteArray[i]));
		}
		return hexString.toString().toLowerCase();
	}

	/**
	 * 16进制转换成为string类型字符串
	 *
	 * @param s
	 * @return
	 */
	public static String hexStringToString(String s) {
		if (s == null || s.equals("")) {
			return null;
		}
		s = s.replace(" ", "");
		byte[] baKeyword = new byte[s.length() / 2];
		for (int i = 0; i < baKeyword.length; i++) {
			try {
				baKeyword[i] = (byte) (0xff & Integer.parseInt(s.substring(i * 2, i * 2 + 2), 16));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		try {
			s = new String(baKeyword, "UTF-8");
			new String();
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		return s;
	}

	/**
	 * isBlank
	 *
	 * @param value
	 * @return true: blank; false: not blank
	 */
	private static boolean isBlank(String value) {
		if (value == null || "".equals(value.trim())) {
			return true;
		}
		return false;
	}

	/**
	 * 判断是否是16进制数
	 *
	 * @param strHex
	 * @return
	 */
	public static boolean isHex(String strHex) {
		int i = 0;
		if (strHex.length() > 2) {
			if (strHex.charAt(0) == '0' && (strHex.charAt(1) == 'X' || strHex.charAt(1) == 'x')) {
				i = 2;
			}
		}
		for (; i < strHex.length(); ++i) {
			char ch = strHex.charAt(i);
			if ((ch >= '0' && ch <= '9') || (ch >= 'A' && ch <= 'F') || (ch >= 'a' && ch <= 'f'))
				continue;
			return false;
		}
		return true;
	}


	/**
	 * 字符串转化成为16进制字符串
	 *
	 * @param s
	 * @return
	 */
	public static String strTo16(String s) {
		String str = "";
		for (int i = 0; i < s.length(); i++) {
			int ch = (int) s.charAt(i);
			String s4 = Integer.toHexString(ch);
			str = str + s4;
		}
		return str;
	}

	public static String format(String str, int length) {
		while (str.length() > length)
			length++;
		while (str.length() < length) {
			str = "0" + str;
		}
		return str;
	}

	public static String format(int num, int length) {
		String str = Integer.toHexString(num);
		return format(str, length);
	}

	//  具体转换过程。
	// 方法中第一个参数为需要转换的数 ，第二参数为位运算&上的数，第三个参数为要移动的二进制位。
	public static void trans(int num, int base, int offset) {
		// 定义一个数组，角标对应数值，可以看做一个查询表。
		char[] chs = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
		char[] arr = new char[32];
		int pos = arr.length;

		if (num == 0) {
			// 如果要转换的数为0，则无需转换。
			System.out.println(0);
		}

		while (num != 0) {
			// 得出指定进制中每一位的数值，存入一个数组中。
			int temp = num & base;
			arr[--pos] = chs[temp];
			// 每得出一个指定进制一个位上的值后，原数值的二进制位就往右移动指定位数，并在前面补零。
			num = num >>> offset; // 移位补零。
		}

		for (int x = pos; x < arr.length; x++) {
			if (x == (arr.length - 1)) {
				System.out.println(arr[x]);
			} else {
				System.out.print(arr[x]);
			}
		}
	}

	/**
	 * URL编码（utf-8）
	 *
	 * @param source
	 * @return
	 */
	public static String urlEncodeUTF8(String source) {
		String result = source;
		try {
			result = java.net.URLEncoder.encode(source, "utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return result;
	}

	// 累加金额
	public static Double buileprice(String mop) {
		return Double.parseDouble(mop);

	}

	public static String getIntersectionList(List<String> list1) {
		String resultString = "";

		if (list1 != null && list1.size() > 0) {
			for (String string : list1) {
				resultString = resultString + string + ",";
			}
			resultString = resultString.substring(0, resultString.length() - 1);
		}
		return resultString;
	}

	// 取出相同集合
	public static List<String> getIntersection(List<String> list1, List<String> list2) {
		List<String> result = new ArrayList<String>();
		for (String integer : list2) {// 遍历list1
			if (list1.contains(integer)) {// 如果存在这个数
				result.add(integer);// 放进一个list里面，这个list就是交集
			}
		}
		return result;
	}


	public static Integer forinteger(String str) {
		if (isNotNullStr(str)) {
			return Integer.parseInt(str);
		} else {
			return 0;
		}

	}

	/**
	 * 时间排序12:00 - 13:00,10:00 - 11:00,11:00 - 12:00
	 */

	public static String formatTime(String time) {
		String[] timeString = StringUtil.obgStrToArray(time.trim(), ",");
		List listStr = new ArrayList<>();

		for (int i = 0; i < timeString.length; i++) {
			listStr.add(timeString[i]);
		}

		Collections.sort(listStr);
		time = "";
		for (int i = 0; i < listStr.size(); i++) {
			time = time + listStr.get(i) + ",";
		}
		time = time.substring(0, time.length() - 1);
		return time;

	}

	// 生成随机数字和字母,
	public static String getStringRandom(int length) {

		String val = "";
		Random random = new Random();

		// 参数length，表示生成几位随机数
		for (int i = 0; i < length; i++) {

			String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
			// 输出字母还是数字
			if ("char".equalsIgnoreCase(charOrNum)) {
				// 输出是大写字母还是小写字母
				int temp = random.nextInt(2) % 2 == 0 ? 65 : 97;
				val += (char) (random.nextInt(26) + temp);
			} else if ("num".equalsIgnoreCase(charOrNum)) {
				val += String.valueOf(random.nextInt(10));
			}
		}
		return val;
	}

	/**
	 * 获得时间
	 */

	public static String getDateFormat(String datastr) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		return df.format(datastr) + "";
	}

	/**
	 * 获得时间
	 */

	public static String getDate() {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Timestamp now = new Timestamp(System.currentTimeMillis());
		return df.format(now);
	}

	public static Timestamp getTimestamp() {
		Timestamp now = new Timestamp(System.currentTimeMillis());
		return now;
	}

	public static String[] obgStrToArray(String str, String split) {
		String[] strArray = null;
		if (isNotNull(str)) {
			strArray = str.split(split); // 拆分字符为"," ,然后把结果交给数组strArray
		}
		return strArray;
	}

	public static String[] convertStrToArray(String str) {
		String[] strArray = null;
		if (isNotNull(str)) {
			strArray = str.split(","); // 拆分字符为"," ,然后把结果交给数组strArray
		}
		return strArray;
	}

	public static String[] StrToArray(String str) {
		String[] strArray = null;
		if (isNotNull(str)) {
			strArray = str.split(" "); // 拆分字符为"," ,然后把结果交给数组strArray
		}
		return strArray;
	}

	/**
	 * 生成订单时间+5位随机数
	 */

	public static String orderidBuile() {
		SimpleDateFormat simpleDateFormat;
		simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
		Date date = new Date();
		String str = simpleDateFormat.format(date);
		Random random = new Random();
		int rannum = (int) (random.nextDouble() * (99999 - 10000 + 1)) + 10000;// 获取5位随机数
		return str + rannum;
	}

	/**
	 * 判断是否为null
	 *
	 * @param str
	 * @return
	 */
	public static boolean isNotNull(String str) {
		return null == str ? false : true;
	}

	/**
	 * 判断是否为空字符串
	 *
	 * @param str
	 * @return
	 */
	public static boolean isNotNullStr(String str) {
		return null != str && !str.trim().equals("") && !str.trim().equals("null") ? true : false;
	}

	/**
	 * 判断两字符串相同
	 *
	 * @param one
	 * @param two
	 * @return
	 */
	public static boolean isEqual(String one, String two) {
		return one.equals(two) && isNotNullStr(one) && isNotNullStr(two);
	}

	/**
	 * 判断数组是否全非空
	 *
	 * @param args
	 * @return true 不为空 false 为空
	 *
	 */
	public static boolean isNotNullArgs(String[] args) {
		if (args == null) {
			return false;
		}
		for (String s : args) {
			if (StringUtil.isNotNullStr(s)) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 八位随机数
	 *
	 * @return
	 */
	public static int randomEight() {
		int j = 0;
		int number = 100000000;
		while (true) {
			boolean flag = true;
			int a = (int) (Math.random() * number);
			if (flag && String.valueOf(a).length() == 8 && a > 10000000 && a < 99999999) {
				j = a;
				break;
			}
		}
		return j;
	}

	/**
	 * int值转成2字节的byte数组
	 *
	 * @param num
	 * @return
	 */
	public static byte[] int2byteArray(int num) {
		byte[] result = new byte[2];
		result[0] = (byte) (num >>> 8); // 取次低8位放到2下标
		result[1] = (byte) (num); // 取最低8位放到3下标
		return result;
	}

	/**
	 * 卡号隐藏********
	 *
	 * @return
	 */
	public static String cardString() {
		String cardNo = "1234567891212";
		StringBuffer sb = new StringBuffer();
		String result = "";
		if (StringUtil.isNotNullStr(cardNo) && cardNo.length() > 6) {
			for (int i = 0; i < cardNo.length() - 6; i++) {
				sb.append("*");
			}
			result = cardNo.substring(0, 2) + sb.toString() + cardNo.substring(cardNo.length() - 4);
		}
		return result;
	}

	/**
	 * 格式化
	 *
	 * @return
	 */
	public static String strFormat(double d) {
		return new DecimalFormat("#0.##").format(d);
	}

	public static String mathRound(String s) {
		if (isNotNullStr(s)) {
			return String.valueOf(Math.round(Float.parseFloat(s)));
		} else {
			return null;
		}

	}

	public static String leftPading(String strSrc, String flag, int strSrcLength) {
		String strReturn = "";
		String strtemp = "";
		int curLength = strSrc.trim().length();
		if (strSrc != null && curLength > strSrcLength) {
			strReturn = strSrc.trim().substring(0, strSrcLength);
		} else if (strSrc != null && curLength == strSrcLength) {
			strReturn = strSrc.trim();
		} else {

			for (int i = 0; i < (strSrcLength - curLength); i++) {
				strtemp = strtemp + flag;
			}

			strReturn = strtemp + strSrc.trim();
		}
		return strReturn;
	}

	// 右边补0
	public static String paddingRightWithZero(Integer length, String src) {
		if (length <= 0 || src == null) {
			return "";
		}

		byte[] byteArray = new byte[length];
		byte[] temp = src.getBytes();
		int len = src.getBytes().length;
		for (int j = 0; j < len; j++) {
			if (temp[j] < 0) {
				if (len >= length) {
					// 兼容双字节字符，避免出现乱码。
					if (length % 2 == 0) {
						len = length;
					} else {
						len = length - 1;
					}
				}
				break;
			}
		}
		System.arraycopy(src.getBytes(), 0, byteArray, 0, len);
		for (int i = len; i < length; i++) {
			byteArray[i] = '0';
		}
		return new String(byteArray);
	}

	/**
	 * 解析XML格式的字符串
	 *
	 * @param strXml
	 * @return
	 */
	public static String parseStrXmlLists(String strXml, String str) {
		if (strXml == null && "".equals(strXml)) {
			return null;
		}
		String result = strXml.substring(strXml.indexOf("<" + str + ">"), strXml.indexOf("</" + str + ">"))
				.replace("<" + str + ">", "");
		return result;
	}

	public static BigInteger stringToInt(String string) {
		int j = 0;
		String str = string.substring(0, string.indexOf("."));
		BigInteger intgeo = new BigInteger(str);
		return intgeo;
	}

	/**
	 * 提供精确的乘法运算。
	 *
	 * @param v1 被乘数
	 * @param v2 乘数
	 * @return 两个参数的积
	 */
	public static double mul(double v1, double v2) {
		BigDecimal b1 = new BigDecimal(Double.toString(v1));
		BigDecimal b2 = new BigDecimal(Double.toString(v2));
		return b1.multiply(b2).doubleValue();
	}

	/**
	 * String 类型数字始终保留两位小数
	 *
	 * @param str
	 * @return
	 */
	public static String twoPointParse(String str) {
		DecimalFormat format = new DecimalFormat("0.00");
		String a = format.format(new BigDecimal(str));
		return a;
	}

	/**
	 * XML格式字符串转换为Map
	 *
	 * @param strXML XML字符串
	 * @return XML数据转换后的Map
	 * @throws Exception
	 */
	public static Map<String, String> xmlToMap(String strXML) throws Exception {
		try {
			Map<String, String> data = new HashMap<String, String>();
			DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
			InputStream stream = new ByteArrayInputStream(strXML.getBytes("UTF-8"));
			org.w3c.dom.Document doc = documentBuilder.parse(stream);
			doc.getDocumentElement().normalize();
			NodeList nodeList = doc.getDocumentElement().getChildNodes();
			for (int idx = 0; idx < nodeList.getLength(); ++idx) {
				Node node = nodeList.item(idx);
				if (node.getNodeType() == Node.ELEMENT_NODE) {
					org.w3c.dom.Element element = (org.w3c.dom.Element) node;
					data.put(element.getNodeName(), element.getTextContent());
				}
			}
			try {
				stream.close();
			} catch (Exception ex) {
				// do nothing
			}
			return data;
		} catch (Exception ex) {
			throw ex;
		}

	}
	public static String hexString2binaryString(String hexString) {
		if (hexString == null || hexString.length() % 2 != 0)
			return null;
		String bString = "", tmp;
		for (int i = 0; i < hexString.length(); i++) {
			tmp = "0000" + Integer.toBinaryString(Integer.parseInt(hexString.substring(i, i + 1), 16));
			bString += tmp.substring(tmp.length() - 4);
		}
		return bString;
	}
	public static  byte[] string2Bytes(String str) {

		if (str==null || str.equals("")) {
			return null;
		}

		str = str.toUpperCase();
		int length = str.length() / 2;
		char[] strChar = str.toCharArray();
		byte[] bt = new byte[length];

		for (int i = 0; i < length; i++) {
			int index = i * 2;
			bt[i] = (byte) (char2Byte(strChar[index]) << 4 | char2Byte(strChar[index + 1]));
		}

		return bt;
	}

	private static byte char2Byte(char ch) {
		return (byte) "0123456789ABCDEF".indexOf(ch);
	}

	public final static long getLong(byte[] bt, boolean isAsc) {
		//BIG_ENDIAN
		if (bt == null) {
			throw new IllegalArgumentException("byte array is null.");
		}
		if (bt.length > 8) {
			throw new IllegalArgumentException("byte array size more than 8.");
		}
		long result = 0;
		if (isAsc)
			for (int i = bt.length - 1; i >= 0; i--) {
				result <<= 8;
				result |= (bt[i] & 0x00000000000000ff);
			}
		else
			for (int i = 0; i < bt.length; i++) {
				result <<= 8;
				result |= (bt[i] & 0x00000000000000ff);
			}
		return result;
	}
// 2byte大小端互换
	public final static String jiaohuan(String str) {
		if (str==null || str.equals("")) {
			return null;
		}

		return str.substring(2, 4)+str.substring(0, 2);
	}
//	字符串转ASCIL
    public static String ConvertToASCII(String string)
    {
        StringBuilder sb = new StringBuilder();
        char[] ch = string.toCharArray();
        for (int i = 0; i < ch.length; i++) {
            sb.append(Integer.valueOf(ch[i]).intValue()).append("");
        }

        return sb.toString();

    }
	/**
	 * 将16进制转换为二进制
	 *
	 * @param hexStr
	 * @return
	 */
	public static byte[] parseHexStr2Byte(String hexStr) {
		if (hexStr.length() < 1)
			return null;
		byte[] result = new byte[hexStr.length() / 2];
		for (int i = 0; i < hexStr.length() / 2; i++) {
			int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
			int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2),
					16);
			result[i] = (byte) (high * 16 + low);
		}
		return result;
	}
	/**
	 * byte数组转换为二进制字符串,每个字节以","隔开
	 **/
	public static String byteArrToBinStr(byte[] b) {
		StringBuffer result = new StringBuffer();
		for (int i = 0; i < b.length; i++) {
			result.append(Long.toString(b[i] & 0xff, 2) + "");
		}
		return result.toString().substring(0, result.length() - 1);
	}

	/**
	 * base64 字符串转换为16进字符串
	 * @param base64Str
	 * @return
	 */
	public static String base64StrToHexStr(String base64Str){
		byte[] decoded = Base64.getDecoder().decode(base64Str);
		return toHexString(decoded);
	}

	/**
	 * 获取匹配的字符串
	 * @param string
	 * @return
	 */
	public static String getMatchString(String string,Pattern pattern){
		Matcher matcher = pattern.matcher(string);
		if(matcher.find()){
			return matcher.group();
		}
		return "";
	}

	/**
	 * 将byte[]转为各种进制的字符串
	 * @param bytes byte[]
	 * @param radix 基数可以转换进制的范围，从Character.MIN_RADIX到Character.MAX_RADIX，超出范围后变为10进制
	 * @return 转换后的字符串
	 */
	public static String binary(byte[] bytes, int radix){
		return new BigInteger(1, bytes).toString(radix);// 这里的1代表正数
	}


	//length用户要求产生字符串的长度
	public static String getRandomString(int length){
		String str="abcde0123456789";
		Random random=new Random();
		StringBuffer sb=new StringBuffer();
		for(int i=0;i<length;i++){
			int number=random.nextInt(15);
			sb.append(str.charAt(number));
		}
		return sb.toString();
	}



}
